//
//  AppStoreTool.m
//  AppStoreStatus
//
//  Created by 巩小鹏 on 2019/6/12.
//  Copyright © 2019 巩小鹏. All rights reserved.
//

#import "AppStoreTool.h"

@implementation AppStoreTool

+(NSDictionary *)infoDictionary{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    return infoDictionary;
}
+(NSString *)appCurVersion{
    NSString *appCurVersion = [[self infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    return appCurVersion;
}
+(NSString *)appCurVersionNum{
    NSString *appCurVersionNum = [[self infoDictionary] objectForKey:@"CFBundleVersion"];
    return appCurVersionNum;
}
+(void)newOpenStrURL:(NSString *)url
{
    [self newOpenURL:[NSURL URLWithString:url]];
}
+(void)newOpenURL:(NSURL *)url
{
    if( [[UIApplication sharedApplication] canOpenURL:url] ) {
        if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 10.0) {
            [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
        }else{
            [[UIApplication sharedApplication] openURL:url];
        }
    }
}
//比较版本的方法，在这里我用的是Version来比较的
+ (BOOL)compareVersionsFormAppStore:(NSString*)AppStoreVersion WithAppVersion:(NSString*)AppVersion
{
    
    BOOL littleSunResult = false;
    
    NSMutableArray* a = (NSMutableArray*) [AppStoreVersion componentsSeparatedByString: @"."];
    NSMutableArray* b = (NSMutableArray*) [AppVersion componentsSeparatedByString: @"."];
    
    while (a.count < b.count) { [a addObject: @"0"]; }
    while (b.count < a.count) { [b addObject: @"0"]; }
    
    for (int j = 0; j<a.count; j++) {
        if ([[a objectAtIndex:j] integerValue] > [[b objectAtIndex:j] integerValue]) {
            littleSunResult = true;
            break;
        }else if([[a objectAtIndex:j] integerValue] < [[b objectAtIndex:j] integerValue]){
            littleSunResult = false;
            break;
        }else{
            littleSunResult = false;
        }
    }
    return littleSunResult;//true就是有新版本，false就是没有新版本
    
}

+(NSString *)upDataTypeFormAppMaxVersion:(NSString *)maxVersion AppMinVersion:(NSString *)minVersion WithAppVersion:(NSString*)AppVersion
{
    //比最小的版本小 强更
    if ([self compareVersionsFormAppStore:minVersion WithAppVersion:AppVersion] == true)
    {//强更
        return @"2";
    }
    else
        //比最大的版本小比最小的版本大或相等 正常更新
//    if (AppVersion < maxVersion && AppVersion > minVersion){
    if ([self compareVersionsFormAppStore:maxVersion WithAppVersion:AppVersion] == true){

        //正常更新
        return @"1";
    }

    
     return @"0";
}



@end
